/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package trombinoscope.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import trombinoscope.modele.Groupe;
import java.sql.*;
import javax.servlet.ServletContext;
import trombinoscope.modele.GroupeDAO;

/**
 *
 * @author jef
 */
@WebServlet(name = "ServletModifierPromo", urlPatterns = {"/ServletModifierPromo"})
public class ServletModifierPromo extends HttpServlet {

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException, ClassNotFoundException, SQLException {
          
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
            String idGroupe = request.getParameter("id");
            int idGroupeInt = Integer.parseInt(idGroupe);
            String nom = request.getParameter("nom");
            String annee = request.getParameter("annee");
            String filiere = request.getParameter("filiere");            
                    
            Groupe g = GroupeDAO.getGroupeByIdGroupe(idGroupeInt);            
            
            Connection con = null;
          //  PreparedStatement ps = null;
          //  ResultSet rs = null;
        try {
           
            Class.forName("com.mysql.jdbc.Driver"); 
            con = DriverManager.getConnection("jdbc:mysql://localhost:3306/trombinoscope","root","");
           /*
            String sql = "UPDATE groupe SET nomGroupe=?, annee=?, filiere=? WHERE idGroupe=?";
            ps = con.prepareStatement(sql);
            
            ps.setString(1, nom);
            ps.setString(2, annee);
            ps.setString(3, filiere);
           ps.setInt(4, idGroupeInt);
            
            ps.executeUpdate();           
      
            */
            g.setNom(nom);
            g.setAnnee(annee);
            g.setFiliere(filiere);
            
            g.save();
            
             ServletContext context = getServletContext();                    
                context.setAttribute("groupe", g);
            
            out.println("<script type='text/javascript'> alert('La promotion a etait modifiée avec succes.') </script>");
            out.println("<script type='text/javascript'> document.location.href='./accueil.jsp' </script>");           
            
            
            
        } finally {            
            out.close();
            con.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            processRequest(request, response);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(ServletModifierPromo.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(ServletModifierPromo.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            processRequest(request, response);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(ServletModifierPromo.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(ServletModifierPromo.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override 
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
